home *** CD-ROM | disk | FTP | other *** search
- /*
- uuencode 36.6 -- Copyright (c) 1993 Ralph Seichter.
-
- Encodes binary files so that they can be posted to the Internet.
-
- This tool is FREELY DISTRIBUTABLE. Copying and spreading is encouraged,
- as long as you don't charge anyone for giving him/her this tool. Please
- note that the Copyright is mine, this is *NOT* public domain software!
- Permission is hereby granted to include the complete (!) archive in all
- non-profit public domain software series like Fred Fish, SaarAG, etc.
-
- This version of uuencode was especially optimized for AmigaDOS 2.04 or
- better and the SAS/C 6.3 development system.
- */
-
- #include <proto/dos.h>
- #include <proto/exec.h>
- #include <proto/utility.h>
-
- #define ARG_FROM 0
- #define ARG_TO 1
- #define ARG_NOCHKSUM 2
- #define ARG_XCHKSUM 3
- #define ARG_TOTAL 4
- #define MODE_NOCHKSUM 0x01
- #define MODE_XCHKSUM 0x02
- #define SIXBIT 0x40 /* 6 bit = 0x40 */
-
- /*
- Encoding (c == 0) returns (0x60).
- Encoding (c > 0) returns ((c & 0x3F) + 0x20).
-
- There is always (0x20 <= c <= 0x60), which means
- that c is guaranteed to be a printable character!
- */
-
- #define ENCODE(c) (c ? (c & 0x3F) + 0x20 : 0x60)
-
-
-
- /* function prototypes and globals ******************************************/
-
- BOOL encodeFile (BPTR, BPTR, STRPTR, BYTE);
- BOOL putFile (BPTR, STRPTR, ...);
- LONG uuencode (VOID);
-
- UBYTE ver[] = "$VER: uuencode 36.6 (24.8.93) Copyright \xA9 1993 Ralph Seichter";
- struct DosLibrary *DOSBase;
-
-
-
- /* the main program *********************************************************/
-
- LONG __saveds uuencode (VOID)
- {
- LONG rc = RETURN_FAIL;
-
- /* uuencode will *NOT* run without dos.library V36 or better! */
- if (DOSBase = (struct DosLibrary *)OpenLibrary (DOSNAME, 36))
- {
- LONG err;
- STRPTR arg[ARG_TOTAL] = {0, 0, 0, 0};
- struct RDArgs *rda;
-
- if (rda = ReadArgs ("FROM/A/M,TO/A/K,NOCHECKSUM/S,XCHECKSUM/S", (LONG *)arg, NULL))
- {
- BPTR out;
- STRPTR *name = (STRPTR *)arg[ARG_FROM];
-
- if (out = Open (arg[ARG_TO], MODE_NEWFILE))
- {
- WORD i;
- BPTR in;
- BYTE mode;
- BOOL ok = TRUE;
-
- mode = (arg[ARG_XCHKSUM] ? MODE_XCHKSUM : 0);
- mode = (arg[ARG_NOCHKSUM] ? MODE_NOCHKSUM : mode);
- for (i = 0; ok && (name[i]); ++i)
- {
- if (in = Open (name[i], MODE_OLDFILE))
- {
- ok = encodeFile (in, out, FilePart (name[i]), mode);
- Close (in);
- }
- }
- Close (out);
- }
- FreeArgs (rda);
- }
- if ((err = IoErr ()) > 0)
- PrintFault (err, NULL);
- else
- rc = RETURN_OK;
- CloseLibrary ((struct Library *)DOSBase);
- }
- return (rc);
- }
-
-
-
- /* encode a complete file ***************************************************/
-
- BOOL encodeFile (BPTR in, BPTR out, STRPTR name, BYTE mode)
- {
- BOOL ok = FALSE;
-
- if (putFile (out, "begin 644 %s\n", name))
- {
- LONG n, i, triple, checksum, size = 0;
- UBYTE c, buf[45], outbuf[80];
- STRPTR s, t;
-
- do
- {
- /* read a maximum of 45 bytes at a time */
- n = FRead (in, buf, 1, 45);
- if (n > 0)
- {
- size += n;
-
- /* the first byte in each output line is the */
- /* encoded number of encoded bytes in the line */
- s = outbuf;
- *s++ = ENCODE(n);
-
- /* now encode data in buffer. three input bytes
- will always result in four output bytes */
- for (checksum = 0, i = 0; i < n; i += 3)
- {
- t = &buf[i];
-
- /* first six bits (7-2) of byte 0 */
- c = t[0] >> 2;
- *s++ = ENCODE(c);
-
- /* last two bits (1-0) of byte 0 OR'ed with first
- four bits (7-4) of byte 1 */
- c = (t[0] << 4) & 0x30 | (t[1] >> 4) & 0x0F;
- *s++ = ENCODE(c);
-
- /* last four bits (3-0) of byte 1 OR'ed with first
- two bits (7-6) of byte 2 */
- c = (t[1] << 2) & 0x3C | (t[2] >> 6) & 0x03;
- *s++ = ENCODE(c);
-
- /* last six bits (5-0) of byte 2 */
- c = t[2] & 0x3F;
- *s++ = ENCODE(c);
-
- /* calculate checksum of current triplet */
- triple = (t[0] + t[1] + t[2]) % SIXBIT;
-
- /* and add this to total line checksum */
- checksum = (checksum + triple) % SIXBIT;
- }
-
- if (!(mode & MODE_NOCHKSUM))
- {
- if (mode & MODE_XCHKSUM)
- *s++ = 'X';
- else
- /* the last byte in each line is the line checksum */
- *s++ = ENCODE(checksum);
- }
-
- /* add a newline character and write line string to file */
- *s++ = '\n';
- *s = 0;
- if (0 != FPuts (out, outbuf))
- n = -1;
- }
- }
- while (n > 0);
- if (n != -1)
- ok = putFile (out, "end\nsize %ld\n\n", size);
- }
- return (ok);
- }
-
-
-
- /* sort of a private fprintf() function *************************************/
-
- BOOL putFile (BPTR file, STRPTR format, ...)
- {
- UBYTE buf[256];
-
- RawDoFmt (format, &format + 1, (void (*))"\x16\xC0\x4E\x75", buf);
- return ((BOOL)(0 == FPuts (file, buf)));
- }
-